package com.bosssoft.user.permission.center.serve.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;

import com.bosssoft.user.permission.center.serve.dao.entity.Resource;
import com.bosssoft.user.permission.center.serve.dao.entity.SubSystem;
import com.bosssoft.user.permission.center.serve.dao.entity.TreeNode;
import com.bosssoft.user.permission.center.serve.dao.mapper.ResourceMapper;
import com.bosssoft.user.permission.center.serve.pojo.dto.LoginDTO;
import com.bosssoft.user.permission.center.serve.pojo.dto.ResourceDTO;

import com.bosssoft.user.permission.center.serve.pojo.query.ResourceQuery;
import com.bosssoft.user.permission.center.serve.pojo.vo.LoginVO;
import com.bosssoft.user.permission.center.serve.pojo.vo.ResourceVO;
import com.bosssoft.user.permission.center.serve.pojo.vo.SubSystemVO;
import com.bosssoft.user.permission.center.serve.service.PageResult;
import com.bosssoft.user.permission.center.serve.service.ResourceService;
import com.bosssoft.user.permission.center.serve.util.LoginTokenUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.page.PageMethod;
import common.annotation.ApiLog;
import common.annotation.AutoFill;
import common.annotation.OperationType;
import common.util.IdGeneratorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 郭其鑫
 */
@Service
@Slf4j
public class ResourceServiceIml implements ResourceService {
    @javax.annotation.Resource
    private ResourceMapper resourceMapper;
    @javax.annotation.Resource
    private IdGeneratorUtil generatorUtil;


    @Override
    @ApiLog
    @AutoFill(OperationType.INSERT)
    public int create(ResourceDTO dto) {
        Resource resource =new Resource();
        log.info(String.valueOf(generatorUtil.snowflakeId()));
        dto.setId(generatorUtil.snowflakeId());
        BeanUtils.copyProperties(dto, resource);
        log.info(resource.toString());
        LoginDTO loginVO = LoginTokenUtil.parseTokenToLoginDTO();
        resource.setCode(loginVO.getCode());
        resource.setTenantId(loginVO.getTenantId());
        resource.setCompanyId(loginVO.getCompanyId());
        resource.setOrgId(loginVO.getOrgId());
        log.info(resource.toString());
        return resourceMapper.insert(resource);
    }

    @Override
    @Transactional
    @ApiLog
    public boolean deleteById(List<Long> idList) {
        for (Long id:idList){
            resourceMapper.deleteById(id);
        }
        return true;
    }

    @Override
    @ApiLog
    @AutoFill(OperationType.UPDATE)
    public int updateById(ResourceDTO dto) {
        Resource resource =new Resource();
        BeanUtils.copyProperties(dto, resource);
        LoginDTO loginVO = LoginTokenUtil.parseTokenToLoginDTO();
        resource.setCode(loginVO.getCode());
        resource.setTenantId(loginVO.getTenantId());
        resource.setCompanyId(loginVO.getCompanyId());
        resource.setOrgId(loginVO.getOrgId());
        Byte status = 1;
        resource.setStatus(status);
        resource.setVersion(1L);
        return resourceMapper.update(resource);
    }

    @Override
    @ApiLog
    public PageResult pageQuery(ResourceQuery resourceQuery) {
        PageMethod.startPage(resourceQuery.getPage(), resourceQuery.getPageSize());
        log.info(resourceQuery.toString());
        Resource resource = new Resource();
        resource.setName(resourceQuery.getName());
        resource.setTenantId(LoginTokenUtil.parseTokenToLoginDTO().getTenantId());
        List<ResourceVO> list;
        long total;
        try (Page<Resource> page = resourceMapper.pageQuery(resource)) {
            list = new ArrayList<>();
            log.info(page.getResult().toString());
            for (Resource r : page.getResult()) {
                ResourceVO resourceVO = new ResourceVO();
                BeanUtils.copyProperties(r, resourceVO);
                log.info(resourceVO.toString());
                list.add(resourceVO);
            }
            log.info(list.toString());
            total = page.getTotal();
        }
        return  new PageResult(total, list);
    }


    @Override
    @ApiLog
    public Map<String, String> getSubsystemList() {
        Map<String,String> map=new HashMap<>();
        for (SubSystem rSubsystemDTO : resourceMapper.getSubsystemName()){
            map.put(rSubsystemDTO.getName(), String.valueOf(rSubsystemDTO.getId()));
        }
        return map;
    }


    @Override
    @ApiLog
    public List<String> getMeau(){
        return resourceMapper.selectMenuPaths();
    }
    @Override
    public List<Tree<String>> getTree() {
        List<TreeNode> list = resourceMapper.selectTree(LoginTokenUtil.parseTokenToLoginDTO().getTenantId());
        log.info(String.valueOf(LoginTokenUtil.parseTokenToLoginDTO().getTenantId()));
        log.info(list.toString());
        List<TreeNode> list2 = CollUtil.newArrayList();
        //浅拷贝赋值
        list2.addAll(list);
        // rootId
        String pid = "-1";
        //配置
        TreeNodeConfig nodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        //设置ID对应的名称
        nodeConfig.setIdKey("id");
        // 最大递归深度 3级目录
        nodeConfig.setDeep(3);
//        入参
//        tree:  最终要返回的数据
//        node:  lists数据
//        返回
//        Tree<String>
//        Tree: 转换的实体 为数据源里的对象类型
//        String: ID类型
        //转换器
        return TreeUtil.build(list2, pid, nodeConfig,
                (node, tree) -> {
                    //id
                    tree.setId(String.valueOf(node.getId()));
                    //姓名
                    tree.setName(node.getName());
                    //获取父节点id
                    tree.setParentId(String.valueOf(node.getParentId()));
                });
    }

    @Override
    public Map<String, Long> getAllParent() {
        Map<String,Long> nameList=new HashMap<>();
        resourceMapper.getAllParent();
        for (Resource resourceDTO : resourceMapper.getAllParent()) {
           nameList.put(resourceDTO.getName(), resourceDTO.getId());
        }
        return nameList;
    }

    @Override
    @Transactional
    public int copyList(List<Long> idList) {
        for (Long id: idList){
            Resource resource =resourceMapper.selectById(id);
            resource.setId(generatorUtil.snowflakeId());
            resourceMapper.insert(resource);
        }
        return 1;
    }



}
